import {
  conditionsQuery,
  addGroup,
  updateGroup
} from '../services/UserForm';
import {
  parse
} from 'qs';
import error, {
  success
} from '../utils/tips';

export default {

  namespace: 'UserForm',

  state: {
    conditionsData: {
      data1: [],
      data2: [],
      data3: [],
      data4: [],
      data5: [],
      data6: []
    },
    conditionsLoading: false,
  },

  subscriptions: {},

  effects: { * conditionsQuery({
      payload
    }, {
      call,
      put
    }) {

      yield put({
        type: 'conditionsLoading'
      });

      const {
        data
      } = yield call(conditionsQuery, parse(payload));

      if (data) {
        let temp = {}
        for (let i = 1; i < 7; i++) {
          let arr = []
          data.data['label' + i] && data.data['label' + i].map(function(item, index) {
            let obj = {};
            obj.value = item.name;
            obj.key = item.classId;
            obj.label = item.name;
            obj.children = [];

            item.value.map(function(item2) {

              let isChildren = Array.isArray(item2.value),
                id = (isChildren ? item2.classId : item.classId) + '__' + item.name + '__' + item2.value,
                children = {
                  label: item2.name,
                  value: id,
                  key: id,
                  children: []
                };
              if (isChildren) {
                item2.value.map(function(item3) {
                  let id = item2.classId + '__' + item2.name + '__' + item3.value + '__' + item3.name;
                  children.children.push({
                    label: item3.name,
                    value: id,
                    key: id,
                  })
                });
              } else {
                children.value = children.value + '__' + item2.name;
              }
              obj.children.push(children);

            });

            arr.push(obj);
          });

          temp['data' + i] = arr
        }

        yield put({
          type: 'conditionsQuerySuccess',
          payload: {
            conditionsData: temp
          },
        });
      } else {
        error('获取筛选条件失败');
      }
    },
    //===================================
    * addGroup({
      payload
    }, {
      call,
      put
    }) {
      const {
        data
      } = yield call(addGroup, parse(payload));

      if (data) {
        if (!data.data) {
          error("创建用户群组失败");
        } else {
          success();
        }
      } else {
        error("创建用户群组异常");
      }
    },
    //===================================
    * updateGroup({
      payload
    }, {
      call,
      put
    }) {
      const {
        data
      } = yield call(updateGroup, parse(payload));

      if (data) {
        if (!data.data) {
          error("修改用户群组失败");
        } else {
          success();
        }
      } else {
        error("修改用户群组异常");
      }
    },

  },

  reducers: {
    conditionsLoading(state) {
      return {...state,
        conditionsLoading: true
      };
    },
    conditionsQuerySuccess(state, action) {
      return {...state,
        ...action.payload,
        conditionsLoading: false
      };
    },

  },
};